﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cua_Hang.DTO;
using System.Data;

namespace Cua_Hang.DAO
{
    public class ChiTiet_SanPham_DAO
    {
        //-------load Chi Tiet San Pham-----------
        public static List<ChiTiet_SanPham_DTO> List_ChiTiet_SanPham_DAO(int ID_ChiTiet_CuaHang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ChiTiet_CuaHang AS T1 JOIN ChiTiet_SanPham AS T2 ON T1.ID_ChiTiet_CuaHang=T2.ID_ChiTiet_CuaHang WHERE T2.ID_ChiTiet_CuaHang like '%" + ID_ChiTiet_CuaHang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ChiTiet_SanPham_DTO> lstChiTiet_SanPham = new List<ChiTiet_SanPham_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTiet_SanPham_DTO dm = new ChiTiet_SanPham_DTO();
                    dm.ID_ChiTiet_CuaHang = (int)dr["ID_ChiTiet_CuaHang"];
                    dm.ID_ChiTiet_SanPham = (int)dr["ID_ChiTiet_SanPham"];
                    dm.Ten_ChiTiet_SanPham = dr["Ten_ChiTiet_SanPham"].ToString();
                    dm.ThongTin_ChiTiet_SanPham = dr["ThongTin_ChiTiet_SanPham"].ToString();
                    dm.Gia_ChiTiet_SanPham = (double)dr["Gia_ChiTiet_SanPham"];
                    dm.GhiChu_ChiTiet_SanPham = dr["GhiChu_ChiTiet_SanPham"].ToString();
                    dm.Anh_ChiTiet_SanPham = dr["Anh_ChiTiet_SanPham"].ToString();
                    

                    lstChiTiet_SanPham.Add(dm);
                }
                return lstChiTiet_SanPham;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load Chi Tiet San Pham-----------
        //===================================================
        //-------load Chi Tiet San Pham theo id cua hang-----------
        public static List<ChiTiet_SanPham_DTO> List_ChiTiet_SanPham_Theo_id_cuahang_DAO(int ID_cuahang)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ChiTiet_CuaHang AS T1 JOIN ChiTiet_SanPham AS T2 ON T1.ID_ChiTiet_CuaHang=T2.ID_ChiTiet_CuaHang WHERE T1.ID_cuahang like '%" + ID_cuahang + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ChiTiet_SanPham_DTO> lstChiTiet_SanPham = new List<ChiTiet_SanPham_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTiet_SanPham_DTO dm = new ChiTiet_SanPham_DTO();
                    dm.ID_ChiTiet_CuaHang = (int)dr["ID_ChiTiet_CuaHang"];
                    dm.Ten_ChiTiet_CuaHang = dr["Ten_ChiTiet_CuaHang"].ToString();
                    dm.ID_ChiTiet_SanPham = (int)dr["ID_ChiTiet_SanPham"];
                    dm.Ten_ChiTiet_SanPham = dr["Ten_ChiTiet_SanPham"].ToString();
                    dm.ThongTin_ChiTiet_SanPham = dr["ThongTin_ChiTiet_SanPham"].ToString();
                    dm.Gia_ChiTiet_SanPham = (double)dr["Gia_ChiTiet_SanPham"];
                    dm.GhiChu_ChiTiet_SanPham = dr["GhiChu_ChiTiet_SanPham"].ToString();
                    dm.Anh_ChiTiet_SanPham = dr["Anh_ChiTiet_SanPham"].ToString();



                    lstChiTiet_SanPham.Add(dm);
                }
                return lstChiTiet_SanPham;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load Chi Tiet San Pham-----------
        //===================================================
        //==============================================================
        //------ xoa chi tiet san pham ------------
        public static void XoaChiTietSP_DAO(ChiTiet_SanPham_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "DELETE FROM ChiTiet_SanPham WHERE ID_ChiTiet_SanPham ='" + ch.ID_ChiTiet_SanPham + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc xoa chi tiet san pham ------------

        //==============================================================
        //==================================================================
        //-----------them chi tiet san pham  ------------

        public static void ThemChiTietSP_DAO(ChiTiet_SanPham_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
                String strSQL = "INSERT INTO ChiTiet_SanPham(Ten_ChiTiet_SanPham,ID_ChiTiet_CuaHang,Anh_ChiTiet_SanPham,Gia_ChiTiet_SanPham,ThongTin_ChiTiet_SanPham,GhiChu_ChiTiet_SanPham) VALUES(";
                strSQL += "N'" + ch.Ten_ChiTiet_SanPham + "',";
                strSQL += "N'" + ch.ID_ChiTiet_CuaHang + "',";
                strSQL += "N'" + ch.Anh_ChiTiet_SanPham+ "',";
                strSQL += "N'" + ch.Gia_ChiTiet_SanPham+ "',";
                strSQL += "N'" + ch.ThongTin_ChiTiet_SanPham + "',";
                strSQL += "N'" + ch.GhiChu_ChiTiet_SanPham + "')";
                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }


        //-------ket thuc them chi tiet cua hang  ------------
        //==============================================================
        //---load Chi Tiet San Pham theo id-----------------------------
        public static List<ChiTiet_SanPham_DTO> List_ChiTiet_TheoId_SanPham_DAO(int ch)
        {
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ChiTiet_CuaHang AS T1 JOIN ChiTiet_SanPham AS T2 ON T1.ID_ChiTiet_CuaHang=T2.ID_ChiTiet_CuaHang WHERE T2.ID_ChiTiet_SanPham like '%" + ch + "%'";
                DataTable dt = d.ExecuteQuery(strSQL);

                List<ChiTiet_SanPham_DTO> lstChiTiet_SanPham = new List<ChiTiet_SanPham_DTO>();
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTiet_SanPham_DTO dm = new ChiTiet_SanPham_DTO();
                    dm.ID_ChiTiet_CuaHang = (int)dr["ID_ChiTiet_CuaHang"];
                    dm.ID_ChiTiet_SanPham = (int)dr["ID_ChiTiet_SanPham"];
                    dm.Ten_ChiTiet_SanPham = dr["Ten_ChiTiet_SanPham"].ToString();
                    dm.ThongTin_ChiTiet_SanPham = dr["ThongTin_ChiTiet_SanPham"].ToString();
                    dm.Gia_ChiTiet_SanPham = (double)dr["Gia_ChiTiet_SanPham"];
                    dm.GhiChu_ChiTiet_SanPham = dr["GhiChu_ChiTiet_SanPham"].ToString();
                    dm.Anh_ChiTiet_SanPham = dr["Anh_ChiTiet_SanPham"].ToString();


                    lstChiTiet_SanPham.Add(dm);
                }
                return lstChiTiet_SanPham;
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc load Chi Tiet San Pham theo id---------
        //===================================================
        //==============================================================
        //------ sua chi tiet sp  ------------
        public static void SuaChiTietSP_DAO(ChiTiet_SanPham_DTO ch)
        {
            try
            {
                DataProvider chang = new DataProvider();
               // String strSQL = "UPDATE ChiTiet_SanPham SET Ten_ChiTiet_CuaHang = N'" + ch.Ten_ChiTiet_CuaHang + "' WHERE ID_ChiTiet_CuaHang = '" + ch.ID_ChiTiet_CuaHang + "'";
                String strSQL = "UPDATE ChiTiet_SanPham SET Ten_ChiTiet_SanPham = N'" + ch.Ten_ChiTiet_SanPham + "',ThongTin_ChiTiet_SanPham = '" + ch.ThongTin_ChiTiet_SanPham + "',ID_ChiTiet_CuaHang = '" + ch.ID_ChiTiet_CuaHang + "',Gia_ChiTiet_SanPham = '" + ch.Gia_ChiTiet_SanPham + "',GhiChu_ChiTiet_SanPham = '" + ch.GhiChu_ChiTiet_SanPham + "' WHERE ID_ChiTiet_SanPham = '" + ch.ID_ChiTiet_SanPham + "'";

               // string strSQL = "UPDATE ChiTiet_SanPham " +
               // "SET Ten_ChiTiet_SanPham = '" + ch.Ten_ChiTiet_SanPham +
               //// " ID_ChiTiet_CuaHang = '" + ch.ID_ChiTiet_CuaHang + "', " +
               //// " Anh_ChiTiet_SanPham = '" + ch.Anh_ChiTiet_SanPham + "', " +
               // " Gia_ChiTiet_SanPham = '" + ch.Gia_ChiTiet_SanPham + "', " +
               // " ThongTin_ChiTiet_SanPham = '" + ch.ThongTin_ChiTiet_SanPham + "', " +
               // " GhiChu_ChiTiet_SanPham = " + ch.GhiChu_ChiTiet_SanPham +
               // " WHERE ID_ChiTiet_SanPham = '" + ch.ID_ChiTiet_SanPham + "'";
               // //" MaLop = '" + info.MaLop + "'," +
               // //" GioiTinh = '" + info.GioiTinh + "', " +
               // //" NamHoc = " + info.NamHoc +
               // //" GhiChu_ChiTiet_SanPham = '" + ch.GhiChu_ChiTiet_SanPham +"'
               // //" WHERE ID_ChiTiet_SanPham = '" + ch.ID_ChiTiet_SanPham + "'";

                chang.ExecuteQuery(strSQL);
            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
        }
        //-------ket thuc sua chi tiet sp  ------------
        //==============================================================
        //==============================================================
        //------ ham kiem tra chi tiet sp co trong csdl hay chua ------------
        public static Boolean KiemTraTrung_ChiTiet_SP_DAO(ChiTiet_SanPham_DTO ch)
        {
            Boolean kq = false;
            try
            {
                DataProvider d = new DataProvider();
                String strSQL = "SELECT * FROM ChiTiet_SanPham  WHERE Ten_ChiTiet_SanPham =N'" + ch.Ten_ChiTiet_SanPham + "'";
                DataTable dt = d.ExecuteQuery(strSQL);
                if (dt.Rows.Count > 0)
                {
                    kq = true;
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Loi!!" + ex.Message);
            }
            return kq;
        }
        //-------ket thuc ham kiem tra chi tiet sp co trong csdl hay chua -----------

        //==============================================================



    }
}